<?
class infoController extends iMVC_Controller{

    protected function before(){

    }

    public function index(){

        $tm=new infoModel();
        $table=new iMVC_View_Table();
        //添加时列名不能为数字，为数字时array_merge会破坏键值
        $table->addCol('删除',new iMVC_View_Table_Col_Action('删除',$this->getBaseUrl()."info/delInfo","info_id"),10);
        $table->addCol('修改',new iMVC_View_Table_Col_Action('修改',$this->getBaseUrl()."info","info_id"),10);
        $table->addCol('修改',new iMVC_View_Table_Col_Action('上传照片',$this->getBaseUrl()."info/uploadPhoto","info_id"),10);
        $table->setCol('info_ico',new Amxx_View_Table_Pic_Action(),11);
        $table->setCol('info_pic',new Amxx_View_Table_Pic_Action(),11);
        $table->generateTable($tm->fetchAll());
        $this->assign('infoTable',$table);
        $this->assign('class_options', array(
                1 => '类别1',
                2 => '类别2',
                3 => '类别3',
                4 => '类别4'));
        //粘性表单
        $id=$_GET['id'];
        if(!empty($id)){
            $info=new infoModel();
            $info->findByPk($id);
            $this->assign('id',$id);
            $this->assign('name',$info->info_name);
            $this->assign('addres',$info->info_addres);
            $this->assign('google',$info->info_google_address);
            $this->assign('ico',$info->info_ico);
            $this->assign('pic',$info->info_pic);
            $this->assign('class_id',$info->info_class);
        }
        $this->display("info.tpl");
    
    }
    
    public function addInfoForm(){

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);

        session_start();
        $user=$_SESSION['user'];
        if(!empty($user)){
            $this->assign('class_options', array(
                1 => '类别1',
                2 => '类别2',
                3 => '类别3',
                4 => '类别4'));
            $this->display("addInfo.tpl");
        }
        else{
            //header("Location:".$this->getBaseUrl()."user/login");
        }
    
    }

    public function addInfo(){

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);

        session_start();
        $user=$_SESSION['user'];
        $user_id=$user->user_id;
        $name=$_POST["name"];
        $class=$_POST["class"];
        $addr=$_POST["address"];
        $google=$_POST["google_address"];
        $ico=$_POST["ico"];
        $pic=$_POST["pic"];
        $detail=$_POST["detail"];
        $city=$_POST["city"];
        $area=$_POST["area"];
        $tel=$_POST["tel"];
        $openTime=$_POST["openTime"];
        $cap=$_POST["cap"];
        $price=$_POST["price"];
        $info=new infoModel();
        if(!empty($user_id)){
            $info->user_id=$user_id;
            $info->info_name=$name;
            $info->info_class=$class;
            $info->info_addres=$addr;
            $info->info_google_address=$google;
            $info->info_ico=$ico;
            $info->info_pic=$pic;
            $info->info_detail=$detail;
            $info->info_tel=$tel;
            $info->info_city=$city;
            $info->info_area=$area;
            $info->info_cap=$cap;
            $info->info_price=$price;
            $info->info_openTime=$openTime;
            $info->save();
            header("Content-Type:text/html;charset=utf-8");
            echo "<script>alert('添加成功');window.location.href='".$this->getBaseUrl()."user/center';</script>";
            //header("Location:".$this->getBaseUrl()."user/center?tip='添加成功'");
        }
        else{
            echo "<script>alert('login first');history.go(-1);</script>";
        }
    
    }
    public function delInfo(){
    
        $id=$_GET['id']; 
        $info=new infoModel();
        $info->info_id=$id;
        var_dump($info);
        $info->del();
        //header("Location:".$this->getBaseUrl()."info");
    
    
    }
    //目前无用已和add合并，最终改为save
    public function updateInfo(){
        $id=$_GET["id"];
        $name=$_POST["name"];
        $class=$_POST["class"];
        $addr=$_POST["address"];
        $google=$_POST["google_address"];
        $ico=$_POST["ico"];
        $pic=$_POST["pic"];
        $info=new infoModel();
        $info->info_id=$id;
        $info->info_name=$name;
        $info->info_class=$class;
        $info->info_addres=$addr;
        $info->info_google_address=$google;
        $info->info_ico=$ico;
        $info->info_pic=$pic;
        $info->save();
        header("Location:".$this->getBaseUrl()."info");
    
    }

    //上传相册照片
    public function uploadPhoto(){
    

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);

        $id=$_GET["id"];
        if(!empty($id)){ 
            $tm=new infoModel();
            $tm->info_id=$id;
            $table=new iMVC_View_Table();
            $rs=$tm->fetchPhotos();
            if($rs){
                $table->generateTable($rs);
            }
            $this->assign("id",$id);
            //$this->assign('rs',$tm->fetchAll());
            $this->assign("table",$table);
        }
        $this->display("photo.tpl");    
    
    
    
    }
    public function shopList(){
    

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);

        session_start(); 
        $page=$_GET['page']; 
        $page=empty($page)?1:$page;
        //$size=$_GET['size'];
        //$page=2;
        $size=8;
        $info=new infoModel();
        $all=ceil(count($info->fetchAll())/$size); 
        $this->assign("rs",$info->fetchLimit(($page-1)*$size,$size,'info_id','desc'));
        $this->assign("all",$all);
        $this->assign("page",$page);
        if($page>0){
            $this->assign("pre",$page-1);
        }
        if($page<$all){
        
            $this->assign("last",$page+1);
        
        }
        $this->display("shop-list.tpl");    
    
    }
     public function single(){

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);
    
        $id=$_GET["id"];
        if(!empty($id)){
            $info=new infoModel();
            $photo=new photoModel();
            $info->findByPk($id);
            $this->assign('id',$id);
            $this->assign('name',$info->info_name);
            $this->assign('addres',$info->info_addres);
            $this->assign('google',$info->info_google_address);
            $this->assign('ico',$info->info_ico);
            $this->assign('pic',$info->info_pic);
            $this->assign('class',$info->info_class);
            $this->assign('tel',$info->info_tel);
            $this->assign('city',$info->info_city);
            $this->assign('area',$info->info_area);
            $this->assign('cap',$info->info_cap);
            $this->assign('openTime',$info->info_opentime);
            $this->assign('detail',$info->info_detail);
            $this->assign('price',$info->info_price);
            if($photo->findByInfo($info)){
                $this->assign("photos",$photo->findByInfo($info));;
            }
            $this->display("single.tpl");    
        }
        else{
            header("Location:".$this->getBaseUrl()."info/shopList"); 
        
        }
    
    }
    public function search(){

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);

        $city=$_REQUEST["city"];
        $class=$_REQUEST["class"];
        $key=$_REQUEST["keyword"];

        if($city=='all'){
        
            $city=null; 
        }
        if($class=='all'){
        
            $class=null; 
        }
        $info=new infoModel();
        $rs=$info->fetchByKeyWord($key,$city,$class);

        if($rs){
            $this->assign("rs",$rs); 
        }
            $this->display("shop-list.tpl");    
    
    }


    public function ajaxSearch(){
    
        $city=$_REQUEST["city"];
        $class=$_REQUEST["class"];
        $key=$_REQUEST["keyword"];
        if($city=='all'){
        
            $city=null; 
        }
        if($class=='all'){
        
            $class=null; 
        }
        $info=new infoModel();
        $rs=$info->fetchByKeyWord($key,$city,$class);
        //var_dump($rs);
        if($rs){
            $this->assign("rs",$rs); 
            $this->display("ajaxSearch.tpl");    
        }
        
    
    }
    

    public function addComment(){
    
        $content=$_POST["content"]; 
        $level=$_POST["level"];
        $id=$_POST["info_id"];
        $time=time();
        $name=$_POST["name"];
        $ct=new commentModel();
        $ct->comment_content=$content;
        $ct->comment_name=$name;
        $ct->info_id=$id;
        //$ct->comment_level=$level;
        $ct->comment_time=$time;
        $ct->save();
        echo "评论成功";
    
    
    }
    public function getComment(){
        $info_id=$_REQUEST["id"];
        $page=$_GET["page"]; 
        $size=2;
        if(empty($page)){
            $page=1;
        }
        $info=new infoModel();
        $info->info_id=$info_id;
        $rs=$info->fetchComment($page,$size);
        $all=ceil(count($info->fetchAllComment())/$size); 
        if($rs){

            $this->assign("id",$info_id);
            $this->assign("rs",$rs);
            $this->assign("all",$all);
            $this->assign("page",$page);
            if($page>1){
                $this->assign("pre",$page-1);
            }
            if($page<$all){
        
                $this->assign("last",$page+1);
        
            }
            $this->display("comment.tpl");
        }
        else{
        
            $this->assign("tips","<p><span class='noComment'>还没有评论</span></p>");
            $this->display("comment.tpl");
        
        }
    
    
    
    
    } 

    


}


// vim600:ts=4 st=4 foldmethod=marker foldmarker=<<<,>>>
// vim600:syn=php commentstring=//%s
